package dobest.ray.com.jiongjiong.activity.HomePage;

import android.content.Context;

import com.android.volley.VolleyError;
import com.dobest.ray.corelibs.logic.BaseLogic;
import com.dobest.ray.corelibs.utils.ToastMgr;

import java.util.List;

import dobest.ray.com.jiongjiong.R;
import dobest.ray.com.jiongjiong.activity.Presenter;
import dobest.ray.com.jiongjiong.bean.ArchiveDetailBean;
import dobest.ray.com.jiongjiong.bean.ArchivesListBean;
import dobest.ray.com.jiongjiong.bean.BaseData;
import dobest.ray.com.jiongjiong.bean.Page;
import dobest.ray.com.jiongjiong.bean.PresenterData;
import dobest.ray.com.jiongjiong.constants.Constant;
import dobest.ray.com.jiongjiong.logicCache.cacheBaseLogic.HomePageManager;

/**
 * Created by Ray on 2016/4/8.
 * https://github.com/ray0807
 */
public class HomePagePresenter extends Presenter {
    private HomePageManager manager;
    private Page currentPage;


    public HomePagePresenter() {
        manager = new HomePageManager();
        currentPage = new Page(0, 15, 0);
    }

    public void getFarVisionData(Context context, String uid) {
        manager.getVisionData(context, uid, new BaseLogic.NListener<BaseData>() {
            @Override
            public void onResponse(BaseData data) {
            }

            @Override
            public void onErrResponse(VolleyError error) {

            }
        });
    }

    /**
     * 获取档案列表
     *
     * @param context
     * @param uid
     * @param isRefresh
     */
    public void getArchiveList(final Context context, String uid, boolean isRefresh) {
        //如果是刷新操作，就重置page
        if (currentPage != null && isRefresh) {
            currentPage = new Page(0, 15, 0);
        }
        manager.getAchiveListData(context, uid, currentPage.pageSize + "", currentPage.currentPage + "", new BaseLogic.NListener<BaseData>() {
            @Override
            public void onResponse(BaseData data) {
                List<ArchivesListBean> ListBean = null;
                if (data.errorCode == Constant.STATUS_CODE_200) {
                    ListBean = data.data.getArchiveList;
                    currentPage = data.page;
                    if (ListBean != null && ListBean.size() == 0) {
                        ToastMgr.show(context.getResources().getString(R.string.common_page_end));

                    }
                    if (currentPage != null) {
                        currentPage.currentPage++;
                    }
                }
                sendData(new PresenterData<List<ArchivesListBean>>(Constant.ARCHIVE_LIST_BEAN_TAG, ListBean));

            }

            @Override
            public void onErrResponse(VolleyError error) {
                sendData(new PresenterData<List<ArchivesListBean>>(Constant.ARCHIVE_LIST_BEAN_TAG, null));
                ToastMgr.show(context.getResources().getString(R.string.common_net_work_error));
            }
        });
    }

    public void getArchiveDetail(final Context context, String uid, String id, String type) {
        manager.getArchiveDetail(context, uid, id, type, new BaseLogic.NListener<BaseData>() {
            @Override
            public void onResponse(BaseData data) {
                ArchiveDetailBean bean = null;
                if (data.errorCode == Constant.STATUS_CODE_200) {
                    bean = data.data.getArchive;
                }
                sendData(new PresenterData<ArchiveDetailBean>(Constant.ARCHIVE_DETAIL_BEAN_TAG, bean));
            }

            @Override
            public void onErrResponse(VolleyError error) {
                ToastMgr.show(context.getResources().getString(R.string.common_net_work_error));
                sendData(new PresenterData<ArchivesListBean>(Constant.ARCHIVE_DETAIL_BEAN_TAG, null));
            }
        });

    }
}
